package com.doform.android.xianggucheng.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.SparseArray;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.doform.android.xianggucheng.IApplication;
import com.doform.android.xianggucheng.R;
import com.doform.android.xianggucheng.service.PushService;
import com.doform.android.xianggucheng.service.TSIntentService;
import com.igexin.sdk.PushManager;
import com.umeng.analytics.MobclickAgent;

import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/5/9.
 */

public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener {

    public static final String TAG = "TAG";
    private SparseArray<View> mViews;
    public Context mContext;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(layoutResId());

        PushManager.getInstance().initialize(this, PushService.class);
        // com.getui.demo.IntentService 为第三方自定义的推送服务事件接收类
        PushManager.getInstance().registerPushIntentService(this, TSIntentService.class);

        ButterKnife.bind(this);

        mViews = new SparseArray<>();

        mContext = this;
        IApplication.getInstance().addActivity(this);


        init();
        initView();
        initData();
        initListener();
    }

    @Override
    protected void onStart() {
        super.onStart();
        updateData();
    }


    protected void updateData() {}

    @Override
    protected void onResume() {
        super.onResume();
        showView();
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    /**
     * 内容view
     *
     * @return
     */
    protected abstract int layoutResId();

    /**
     * 初始化
     */
    protected void init(){};

    /**
     * 初始化view
     */
    protected void initView(){};


    /**
     * 初始化数据
     */
    protected void initData(){};

    /**
     * 初始化监听
     */
    protected void initListener(){};

    protected abstract void showView();

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_back:
                hintKbTwo();
                finish();
                break;
            default:
                processClick(v);
                break;
        }
    }

    /**
     * 点击事件
     *
     * @param v
     */
    protected void processClick(View v){};

    public <E extends View> E findView(int id) {
        E view = (E) mViews.get(id);
        if (view == null) {
            view = (E) findViewById(id);
            mViews.put(id, view);
        }
        return view;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    //此方法只是关闭软键盘
    private void hintKbTwo() {
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        if(imm.isActive()&&getCurrentFocus()!=null){
            if (getCurrentFocus().getWindowToken()!=null) {
                imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    }
}
